#!/usr/bin/perl -w
# 
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 07/07/2009 Created by Zhixin Li
# -- Used to collect mysql backup configuration file 
#  -- port|bakday|method|type|compress|slave|status
# -- em_result=3306|0|0|0|0|0|0   
#    2 no backup schedule  normal status
#    1 normal status
#    0 duplicate value of port_method_type or dayofweek error   
use strict;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Down${DELIM}Down${DELIM}Down${DELIM}Down${DELIM}Down${DELIM}Down${DELIM}0\n";
    exit 1;
}

my $SQL1="show variables like 'port'";
my $m_port=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $myport = substr($m_port,index($m_port,"\t")+1,length($m_port)-index($m_port,"\t")+1);
chomp($myport);
my $home;
@ARGV = ("/etc/passwd");
while (<>) {
  if (/^mysql/) {
     $home = (split(/:/,$_))[5];
  }
}
my $bakconf = "$home/dbadmin/conf/dbbackup_sched.dat";
if (! -e "$bakconf" || -z "$bakconf") {
   print "em_result=$myport${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}2\n";
   exit 0;
}

my $rownum =0;
my @contents = `/bin/cat $bakconf|/bin/grep -v "#"`;
chomp(@contents);
my %ucnt = ( );
for (@contents) {
    my ($po,$tm,$mt,$tp,$cmp,$slave) = (split (/[ \t]+/, $_));
    if (/^$myport/) {
       $rownum++;
       my $match = $po.$mt.$tp;
       $ucnt{$match}++;
    }
}
my @result = sort keys %ucnt;
my $lines = scalar(@result); 
if ("$lines" ne "$rownum") {
   print "em_result=$myport${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}0\n";
   exit 0;
} else {
   my $occurence = 0;
   my $rows = scalar(@contents);     #backup conf file lines excluded comment lines
   for (@contents) {
       chomp($_);
       my ($po,$tm,$mt,$tp,$cmp,$slave) = (split (/[ \t]+/, $_));
       #if (/^$myport/) {
       if ($po eq $myport) {
          my @sched = (split (/,/, $tm)); 
          my $freq = scalar(@sched);     #times with scheduled day
          my $tm_freq = 0;
          for (@sched) {
              chomp($_);
              my $re = $_;
              my $grep = grep(/$re/,"Mon,Tue,Wed,Thu,Fri,Sat,Sun");
              if ($grep == 1) {
                 $tm_freq++;
              } 
          }
          if ($tm_freq == $freq) {
              print "em_result=$po$DELIM$tm$DELIM$mt$DELIM$tp$DELIM$cmp$DELIM$slave${DELIM}1\n";
          } else {
              print "em_result=$po$DELIM$tm$DELIM$mt$DELIM$tp$DELIM$cmp$DELIM$slave${DELIM}0\n";
          }
       } else {
          $occurence++; 
       }
   }
   if ($occurence == $rows) { # if no matched port, means do not schedule backup
       print "em_result=$myport${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}0${DELIM}2\n";
       exit 0;
   }
}
